//-----------------------------------------------------------------------
// <copyright file="a_AlbumDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_AlbumDAOBase
    {
        #region Common methods
        public virtual a_Album Createa_AlbumFromReader(IDataReader reader)
        {
            a_Album item = new a_Album();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ID"))) item.ID = (int)reader["ID"];
                if (!reader.IsDBNull(reader.GetOrdinal("UserID"))) item.UserID = (int)reader["UserID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("AlbumType"))) item.AlbumType = (bool)reader["AlbumType"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatPopularity"))) item.StatPopularity = (int)reader["StatPopularity"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_Album methods
            
        public virtual void Createa_Album(a_Album a_Album)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Albums_ins");
                
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_Album.UserID);
                database.AddInParameter(dbCommand, "@Name", DbType.String, a_Album.Name);
                database.AddInParameter(dbCommand, "@AlbumType", DbType.Boolean, a_Album.AlbumType);
                database.AddInParameter(dbCommand, "@StatPopularity", DbType.Int32, a_Album.StatPopularity);
                database.AddOutParameter(dbCommand, "@ID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_Album.ID = (int)database.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_Album methods
        
        public virtual void Updatea_Album(a_Album a_Album)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Albums_upd");            
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, a_Album.ID);
                database.AddInParameter(dbCommand, "@UserID", DbType.Int32, a_Album.UserID);
                database.AddInParameter(dbCommand, "@Name", DbType.String, a_Album.Name);
                database.AddInParameter(dbCommand, "@AlbumType", DbType.Boolean, a_Album.AlbumType);
                database.AddInParameter(dbCommand, "@StatPopularity", DbType.Int32, a_Album.StatPopularity);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_Album methods
        public virtual void Deletea_Album(int id)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Albums_del");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_Album methods
        
        public virtual a_Album Geta_Album(int id)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Albums_sel");
                
                database.AddInParameter(dbCommand, "@ID", DbType.Int32, id);
                
                a_Album a_Album =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_Album = Createa_AlbumFromReader(reader);
                        reader.Close();
                    }
                }
                return a_Album;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_AlbumList methods
        public virtual a_AlbumCollection Geta_AlbumList(a_AlbumColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Albums_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_AlbumCollection a_AlbumCollection = new a_AlbumCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Album a_Album = Createa_AlbumFromReader(reader);
                        a_AlbumCollection.Add(a_Album);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_AlbumCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_AlbumCollection Geta_AlbumList(a_AlbumColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_AlbumList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion

        #region Geta_AlbumListBySSID methods
        public virtual a_AlbumCollection Geta_AlbumListBySSID( string ssid, a_AlbumColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_Albums_selListBySSID");
                database.AddInParameter(dbCommand, "@ssid", DbType.AnsiString, ssid);
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);

                a_AlbumCollection a_AlbumCollection = new a_AlbumCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_Album a_Album = Createa_AlbumFromReader(reader);
                        a_AlbumCollection.Add(a_Album);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_AlbumCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public virtual a_AlbumCollection Geta_AlbumListBySSID(string ssid, a_AlbumColumns orderBy, OrderDirection orderDirection)
        {
            int totalRecords = 0;
            return Geta_AlbumListBySSID(ssid,orderBy, orderDirection, 0, 0, out totalRecords);
        }

        #endregion
    }
}